package handle

import (
	"context"
	cost "gitee.com/jinmingzhi/cdce-service/api/protos/cost/v1alpha"
	"gitee.com/jinmingzhi/cdce-service/internal/service"
	"github.com/golang/protobuf/ptypes/empty"
)

type Cost struct {
	PaySvc service.Pay
}

func NewCost(pay service.Pay) *Cost {
	return &Cost{PaySvc: pay}
}

// 网上缴费
func (c *Cost) PayList(ctx context.Context, _ *empty.Empty) (*cost.PayListResp, error) {
	uid, err := GetUid(ctx)
	if err != nil {
		return nil, handlerErr(err)
	}
	data, err := c.PaySvc.FindAll(ctx, uid)
	if err != nil {
		return nil, handlerErr(err)
	}

	return paysTopbListResp(data), nil
}

// 确认缴费
func (c *Cost) Confirm(ctx context.Context, req *cost.ConfirmReq) (*empty.Empty, error) {
	uid, err := GetUid(ctx)
	if err != nil {
		return nil, handlerErr(err)
	}
	err = c.PaySvc.ConfirmCost(ctx, req.PayId, uid)
	if err != nil {
		return nil, handlerErr(err)
	}
	return &empty.Empty{}, nil
}

// 缴费信息查询
func (c *Cost) PayComplated(ctx context.Context, _ *empty.Empty) (*cost.PayComplatedResp, error) {
	uid, err := GetUid(ctx)
	if err != nil {
		return nil, handlerErr(err)
	}
	data, err := c.PaySvc.FindAllComplate(ctx, uid)
	if err != nil {
		return nil, handlerErr(err)
	}

	return paysTopbComplatedResp(data), nil
}
